package com.seek.eduservice.controller;

import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.entity.EduCourse;
import com.seek.eduservice.entity.vo.CoursePublishVo;
import com.seek.eduservice.entity.vo.CourseQuery;
import com.seek.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-01-16
 */
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    // 添加课程基本信息的方法
    @PostMapping("add")
    public ResponseResult add(@RequestBody EduCourse eduCourse) {
        eduCourseService.save(eduCourse);
        return ResponseResult.ok().data("courseId", eduCourse.getId());
    }

    // 根据课程 id 查询课程基本信息的方法
    @GetMapping("get/{courseId}")
    public ResponseResult get(@PathVariable String courseId) {
        EduCourse eduCourse = eduCourseService.getById(courseId);
        return ResponseResult.ok().data("eduCourse", eduCourse);
    }

    // 根据课程 id 修改课程基本信息的方法
    @PutMapping("update")
    public ResponseResult update(@RequestBody EduCourse eduCourse) {
        eduCourseService.updateById(eduCourse);
        return ResponseResult.ok();
    }

    // 根据课程 id 查询课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public ResponseResult getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(courseId);
        return ResponseResult.ok().data("publishCourse", coursePublishVo);
    }

    // 课程最终发布，根据课程 id 修改课程状态
    @PutMapping("updateCourseStatus/{courseId}")
    public ResponseResult updateCourseStatus(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return ResponseResult.ok();
    }

    // 查询所有课程
    @GetMapping
    public ResponseResult getAll() {
        List<EduCourse> list = eduCourseService.list();
        return ResponseResult.ok().data("records", list);
    }

    // 根据条件查询课程信息带分页
    @GetMapping("pageCondition")
    public ResponseResult pageCondition(CourseQuery courseQuery) {
        int total = eduCourseService.count(courseQuery);
        List<CourseQuery> records = eduCourseService.getPageCondition(courseQuery);
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 删除课程
    @DeleteMapping("deleteById/{courseId}")
    public ResponseResult deleteById(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return ResponseResult.ok();
    }

    // 根据浏览量降序查询十条数据
    @GetMapping("getPopularCourse")
    public ResponseResult getPopularCourse() {
        Map<String, Object> map = eduCourseService.getPopularCourse();
        return ResponseResult.ok().data(map);
    }

}

